package com.qigao.mall.web.controller.app.merchant;

import com.qigao.mall.api.ShopEmpService;
import com.qigao.mall.commons.dto.ShopEmpCreateDto;
import com.qigao.mall.commons.dto.ShopEmpResultDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.web.controller.AbstractShopEmpController;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.utils.ValidatorUtil;
import com.qigao.mall.web.vo.app.ShopEmpCreateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/app/merchant/shopEmp/")
@Api(tags = "App商户店铺员工管理")
public class ShopEmpController extends AbstractShopEmpController {

    private static final Logger logger = LoggerFactory.getLogger(ShopEmpController.class);
    @Resource
    private ShopEmpService shopEmpService;
    //1.新增店铺员工
    @PostMapping("/create")
    @ApiOperation(value = "新增店铺员工", notes = "新增店铺员工")
    public Result create(@RequestBody ShopEmpCreateVo shopEmpCreateVo, HttpServletRequest request){
        ValidatorUtil.validateEntity(shopEmpCreateVo);
        try{
            ShopEmpCreateDto shopEmpCreateDto=new ShopEmpCreateDto();
            BeanUtils.copyProperties(shopEmpCreateVo,shopEmpCreateDto);
            shopEmpCreateDto.setOperNo(RequestUtils.getCurrentUserNo(request));
            shopEmpService.create(shopEmpCreateDto);
            return Result.newSuccess();
        }catch (Exception e){
            logger.error("新增店铺员工发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    //2.启用禁用店铺员工
    @PostMapping("/enableDisable/{id}")
    public Result enableDisable(@PathVariable("id")Long id, HttpServletRequest request){
        try{
            shopEmpService.enableDisable(id,RequestUtils.getCurrentUserNo(request));
            return Result.newSuccess();
        }catch (Exception e){
            logger.error("启用禁用发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    //3.按照店铺号查询员工列表
    @GetMapping("/findListByShopNo/{shopNo}")
    public Result findListByShopNo(@PathVariable("shopNo")String shopNo){
        try{
            List<ShopEmpResultDto> shopEmpResultDtos=shopEmpService.findListByShopNo(shopNo);
            if(shopEmpResultDtos==null || shopEmpResultDtos.size()<=0){
                return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
            }
            return Result.newSuccess(shopEmpResultDtos);
        }catch (Exception e){
            logger.error("按照店铺号查询员工列表异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

}
